import { createRouter, createWebHashHistory } from 'vue-router';

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: () => import('../views/layout.vue'),
      beforeEnter(_to, _from, next) {
        const token = useUserStore().token;
        if (token) {
          next();
        } else {
          next('/login');
        }
      },
      redirect: '/home',
      children: [
        {
          path: '/home',
          name: 'home',
          component: () => import('../views/home/index.vue'),
        },
        {
          path: '/chat',
          name: 'chat',
          component: () => import('../views/chat/index.vue'),
        },
      ],
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login.vue'),
    },
  ],
});

export default router;
